<?php

/**
 * This is the model class for table "user_position_level".
 *
 * The followings are the available columns in table 'user_position_level':
 * @property string $id
 * @property string $unique_id
 * @property integer $L
 * @property integer $M
 * @property integer $B
 */
class UserPositionLevel extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return UserPositionLevel the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user_position_level';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('unique_id,attr_L_id,attr_M_id, B', 'required'),
			//array('B', 'numerical', 'integerOnly'=>true),
			//array('unique_id', 'length', 'max'=>32),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, unique_id, attr_L_id, attr_M_id, B ,status ,dateline', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'unique_id' => 'Unique',
			'attr_L_id' => 'attr_L_id',
			'attr_M_id' => 'attr_M_id',
			'B' => 'B',
			'title' => 'title',
			'title_en' => 'title_en',
			'status' => 'status',
			'dateline' => 'dateline',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('unique_id',$this->unique_id,true);
		$criteria->compare('attr_L_id',$this->attr_L_id,true);
		$criteria->compare('attr_M_id',$this->attr_M_id);
		$criteria->compare('B',$this->B);
		$criteria->compare('status',$this->status);
		$criteria->compare('dateline',$this->dateline);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function checkBrand($attrLId , $attrMId)
	{
		//L
		if($attrLId != 0)
			$L = HrPositionLevel::model()->findByAttributes(array('id'=>$attrLId));
		//M
		if($attrMId != 0)
			$M = HrPositionLevel::model()->findByAttributes(array('id'=>$attrMId));

		if($M)
			return max($M->brand,$L->brand);
		else
			return $L->brand;
	}

	//将之前的状态置为0
	public function updateStatus($uniqueId)
	{
		return UserPositionLevel::model()->updateAll(array('status'=>'-1'),"unique_id='{$uniqueId}'");
	}

	//根据uniqueId获取
	public function getByUniqueId($uniqueId)
	{
		return UserPositionLevel::model()->findByAttributes(array('status' => '1',"unique_id" => $uniqueId));
	}


}